package com.example.study.commen.print;

import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import javax.print.PrintException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CusUtils {
    /**
     * @param in  传入完整文件名  包含后缀
     * @param out 完整文件名   包含后缀
     * @throws IOException
     */
    public static void fileConversion(String in, String out) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(in);
        XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
        PdfOptions pdfOptions = PdfOptions.create();
        FileOutputStream fileOutputStream = new FileOutputStream(out);
        PdfConverter.getInstance().convert(xwpfDocument, fileOutputStream, pdfOptions);
        fileInputStream.close();
        fileOutputStream.close();


    }

    /**
     * 获取文件后缀名
     */
    public static String getFileSuffix(File file) {
        String filename = file.getName();
        String[] strArray = filename.split("\\.");
        int suffixIndex = strArray.length - 1;
        System.out.println(strArray[suffixIndex]);
        return strArray[suffixIndex];
    }


    /**
     * @param pathname    完整文件名
     * @param printerName "Generic / Text Only"
     */
    public static void callPrinter(String pathname, String printerName) {
        DrivePrintService drivePrintService = new DrivePrintService();
        File file = new File(pathname);
        File[] files = new File[]{file};
        try {
            drivePrintService.print(files, printerName);

        } catch (PrintException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
